﻿using System;
using System.Drawing;
using System.Windows.Forms;



namespace FXnaGis
{
    public class FZoomMF:FMapFunction
    {


        public FZoomMF(FMapFrame frame)
            : base(frame)
        {

            Configure();
 
        }





        private Rectangle _client;
        private int _direction;
    
        private double _sensitivity;
        private int _timerInterval;
        private Timer _zoomTimer;


        /// <summary>
        /// Gets or sets the wheel zoom sensitivity.  Increasing makes it more sensitive.  Maximum is 0.5, Minimum is 0.01
        /// </summary>
        public double Sensitivity
        {
            get { return 1.0 / _sensitivity; }
            set
            {
                if (value > 0.5)
                    value = 0.5;
                else if (value < 0.01)
                    value = 0.01;
                _sensitivity = 1.0 / value;
            }
        }

        /// <summary>
        /// Controls the sense (direction) of zoom (in or out) as you roll the mouse wheel
        /// </summary>
        public bool ForwardZoomsIn
        {
            get { return _direction > 0; }
            set { _direction = value ? 1 : -1; }
        }

        /// <summary>
        /// Gets or sets the full refresh timeout value in milliseconds
        /// </summary>
        public int TimerInterval
        {
            get { return _timerInterval; }
            set
            {
                _timerInterval = value;
                _zoomTimer.Interval = _timerInterval;
            }
        }



        private void Configure()
        {
            //YieldStyle = YieldStyles.Scroll;

            this._alwaysOn = true;


            _timerInterval = 100;
            _zoomTimer = new Timer { Interval = _timerInterval };
            _zoomTimer.Tick += ZoomTimerTick;
            _client = Rectangle.Empty;
            Sensitivity = 0.5;
            ForwardZoomsIn = true;

            //Name = "ScrollZoom";


        }

        private void ZoomTimerTick(object sender, EventArgs e)
        {
            _zoomTimer.Stop();
            if (this._geoMapFrame == null)
            {
                return;
            }
            _client = Rectangle.Empty;
            _geoMapFrame.ResetExtents();


           
        }






        protected override void OnMouseWheel(System.Windows.Forms.MouseEventArgs e)
        {


            _zoomTimer.Stop(); // if the timer was already started, stop it.
            Rectangle r =this._geoMapFrame.View;

            // For multiple zoom steps before redrawing, we actually
            // want the x coordinate relative to the screen, not
            // the x coordinate relative to the previously modified view.
            if (_client == Rectangle.Empty)
            {
                _client = r;
            }
            int cw = _client.Width;
            int ch = _client.Height;

            double w = r.Width;
            double h = r.Height;

            if (_direction * e.Delta > 0)
            {
                double inFactor = 2.0 * _sensitivity;
                r.Inflate(Convert.ToInt32(-w / inFactor), Convert.ToInt32(-h / inFactor));
                // try to keep the mouse cursor in the same geographic position
                r.X += Convert.ToInt32((e.X * w / (_sensitivity * cw)) - w / inFactor);
                r.Y += Convert.ToInt32((e.Y * h / (_sensitivity * ch)) - h / inFactor);
            }
            else
            {
                double outFactor = 0.5 * _sensitivity;
                r.Inflate(Convert.ToInt32(w / _sensitivity), Convert.ToInt32(h / _sensitivity));
                r.X += Convert.ToInt32(w / _sensitivity - (e.X * w / (outFactor * cw)));
                r.Y += Convert.ToInt32(h / _sensitivity - (e.Y * h / (outFactor * ch)));
            }

            this._geoMapFrame.View = r;
            this._geoMapFrame.Parent.Invalidate();
            _zoomTimer.Start();
          
          



            base.OnMouseWheel(e);




        }


    }


}
